import {createRouter, createWebHashHistory} from "vue-router";
import layout from "../views/layout.vue";

const routes = [
    {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import ("../views/adminLogin.vue")
    },
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: "/",
        name: "home",
        component: layout,
        children: [
            {
                path: '/home',
                name: 'home',
                meta: {
                    title: '我的首页'
                },
                component: () => import ('../views/myhome.vue')
            },
            {
                path: "/newstable",
                name: "newstable",
                meta: {
                    title: '新闻列表'
                },
                component: () => import ("../views/news/NewsTable.vue")
            },
            {
                path: "/comment",
                name: "comment",
                meta: {
                    title: '评论列表'
                },
                component: () => import ("../views/comment/CommentTable.vue")
            },
            {
                path: "/user",
                name: "user",
                meta: {
                    title: '用户列表'
                },
                component: () => import ("../views/user/UserTable.vue")
            },
            {
                path: "/news/create",
                name: "news-create",
                meta: {
                    title: '新闻发布'
                },
                component: () => import ("../views/news/CreateOrEdit.vue")
            },
            {
                path: "/news/:newsId/edit",
                name: "news-edit",
                props:true,
                meta: {
                    title: '新闻编辑'
                },
                component: () => import ("../views/news/CreateOrEdit.vue")
            },
            {
                path: "/admintable",
                name: "admintable",
                meta: {
                    title: '管理员列表'
                },
                component: () => import ("../views/adminTable.vue")
            },
            {
                path: "/jsonFormat",
                name: "jsonFormat",
                meta: {
                    title: 'json格式化'
                },
                component: () => import ("../views/jsonViewers.vue")
            },
        ]
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    // document.title = `${to.meta.title} | vue-manage-system`;
    const role = localStorage.getItem('user_name');
    // let token = sessionStorage.getItem('token')
    console.log(role)
    if (!role && to.path !== '/login' && to.path !== '/register') {
        next('/login');
    }else {
        next();
    }
});

export default router;
